<template>
  <el-card>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item label="属性组" prop="propId">
        <el-select v-model="state.queryForm.propGroupId" style="width: 100%" placeholder="请选择属性组"
                   @change="getDataList">
          <el-option v-for="ds in state.propGroupList" :key="ds.value" :label="ds.label" :value="ds.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">刷新</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="cloneGroupHandle()">克隆</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.queryForm.propGroupId !== '1'" type="danger" @click="deleteBatchHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%"
              @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" label="属性名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="value" label="属性值" header-align="center" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注信息" header-align="center" align="center"></el-table-column>
      <el-table-column v-if="state.queryForm.propGroupId !== '1'" label="操作" fixed="right" header-align="center"
                       align="center" width="150">
        <template #default="scope">
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="state.pageNum"
        :page-sizes="state.pageSizes"
        :page-size="state.pageSize"
        :total="state.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
    <clone-group ref="cloneGroupRef" @refreshDataList="getDataList"></clone-group>
  </el-card>
</template>

<script setup lang="ts">
import {useCrud} from '@/hooks'
import {onMounted, reactive, ref} from 'vue'
import AddOrUpdate from './add-or-update.vue'
import {usePropGroupOptionsApi} from "@/api/prop";
import {IPropItemOptions} from "@/types/prop";
import CloneGroup from "@/views/prop/clone-group.vue";

const state: IPropItemOptions = reactive({
  createdIsNeed: false,
  dataListUrl: '/generator/v1/propItem/page',
  deleteUrl: '/generator/v1/propItem',
  queryForm: {
    propGroupId: ''
  },
  propGroupList: []
})

onMounted(() => {
  // 查询全部的属性组
  usePropGroupOptionsApi().then(res => {
    state.propGroupList = res.data
    // 设置属性组
    state.queryForm!!.propGroupId = state.propGroupList[0].value
    getDataList()
  })
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id)
}

const cloneGroupRef = ref()
const cloneGroupHandle = () => {
  cloneGroupRef.value.init()
}

const {getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle} = useCrud(state)
</script>
